const values_default = {
  state: 'draft'
}

const domain_default = {}

const fields_default = {
  state: null,
  // can_reset: null,
  // payment_mode: null,
  name: null,
  accounting_date: null,
  employee_id: null,
  total_amount: null,
  user_id: null,
  department_id: null,
  company_id: null,
  expense_line_ids: {
    name: null,
    date: null,
    employee_id: null,
    product_id: null,
    unit_amount: null,
    total_amount: null
  }
}

const filterItems = [
  { type: 'input', name: 'name___like', placeholder: '名称' },
  { type: 'date', name: 'accounting_date', placeholder: '账期' },
  { type: 'select', name: 'employee_id', placeholder: '员工' }
]

const columns = [
  { name: 'expense_line_ids__count', type: 'char', label: '明细条数' },
  { name: 'state__name', type: 'char', label: '状态' },
  // { name: 'can_reset', type: 'boolean', label: '可重置' },
  // { name: 'payment_mode', type: 'char', label: '支付方式' },
  { name: 'accounting_date', type: 'char', label: '日期' },
  { name: 'employee_id__name', type: 'char', label: '员工' },
  { name: 'total_amount', type: 'char', label: '金额' },
  { name: 'name', type: 'char', label: '摘要' },
  { name: 'user_id__name', type: 'char', label: '管理员' },
  { name: 'department_id__name', type: 'char', label: '部门' },
  { name: 'company_id__name', type: 'char', label: '公司' }
]

const formItems = [
  { type: 'input', name: 'name', label: '摘要' },
  { type: 'date', name: 'accounting_date', label: '日期' },
  {
    type: 'select',
    name: 'employee_id',
    options: {
      fields: { name: null, display_name: null, department_id: null }
    },
    label: '员工',
    placeholder: '员工'
  },
  {
    type: 'select',
    name: 'user_id',
    label: '管理员',
    rules: { required: false },
    placeholder: '管理员'
  }
  // {
  //   type: 'select2',
  //   name: 'payment_mode',
  //   label: '支付方式',
  //   placeholder: '支付方式'
  // }
]

const child_configs = {
  expense_line_ids: {
    columns: [
      { name: 'name', type: 'char', label: '说明' },
      { name: 'date', type: 'char', label: '日期' },
      { name: 'product_id__name', type: 'char', label: '费用类型' },
      { name: 'unit_amount', type: 'char', label: '金额' }
    ],

    formItems: [
      { type: 'input', name: 'name', label: '说明' },
      { type: 'date', name: 'date', label: '日期' },

      {
        type: 'select',
        name: 'employee_id',
        label: '员工',
        default: params => {
          const { headData } = params
          return headData.employee_id
        },
        placeholder: '员工',
        hidden: { create: true, update: true }
      },

      {
        type: 'select',
        name: 'product_id',
        label: '费用类型',
        placeholder: '费用类型'
      },
      {
        type: 'number',
        name: 'unit_amount',
        label: '金额',
        precision: 2,
        step: 1,
        default: 0,
        placeholder: '金额'
      }
    ]
  }
}

const workflow = {
  states: {
    draft: '起草',
    submit: '提交',
    approve: '审批',
    post: '记账',
    done: '支付',
    cancel: '已拒绝'
  },

  actions: {
    update: { value: '', name: '修改', type: 'primary' },
    draft: {
      name: '重置为草稿',
      method: 'reset_expense_sheets',
      last_in: ['submit', 'cancel'],
      odoo_type: 'object',
      type: 'primary'
    },
    submit: {
      name: '提交',
      method: 'action_submit_sheet',
      last_in: ['draft'],
      odoo_type: 'object',
      type: 'primary'
    },
    approve: {
      name: '批准',
      method: 'approve_expense_sheets',
      last_in: ['submit'],
      type: 'primary'
    },
    post: {
      name: '记账',
      method: 'action_sheet_move_create',
      last_in: ['approve'],
      type: 'primary'
    },
    done: {
      name: '支付',
      last_in: ['post'],
      type: 'primary',
      // method: 'paid_expense_sheets',
      odoo_type: 'action',
      wizard: {
        model: 'hr.expense.sheet.register.payment.wizard',
        vals: {
          journal_id: params => {
            const { wizard } = params
            const journal_id_options = wizard.formItems.filter(
              item => item.name === 'journal_id'
            )[0].options_for_select_init

            return journal_id_options[0].id
          },

          payment_method_id: params => {
            const { wizard } = params
            const journal_id_options = wizard.formItems.filter(
              item => item.name === 'journal_id'
            )[0].options_for_select_init

            return journal_id_options[0].outbound_payment_method_ids[0]
          }
        },
        context: {
          default_amount: params => {
            const { record } = params
            return record.total_amount
          }
        },

        onchange: ['_onchange_partner_id', '_onchange_journal'],

        fields: {
          journal_id: null,
          amount: null,
          payment_date: null,
          communication: null
        },
        formItems: [
          { type: 'input', name: 'amount', label: '金额' },
          { type: 'date', name: 'payment_date', label: '日期' },
          {
            type: 'input',
            name: 'communication',
            label: '备注',
            rules: { required: false }
          },
          {
            type: 'select',
            name: 'journal_id',
            label: '支付方式',
            options: {
              fields: {
                outbound_payment_method_ids: { name: null }
              }
            }
          }
        ],
        method: 'expense_post_payment'
      }
    },
    cancel: {
      name: '拒绝',
      last_in: ['approve', 'submit'],
      // method: 'refuse_sheet',
      odoo_type: 'action',
      wizard: {
        model: 'hr.expense.refuse.wizard',
        vals: { reason: '' },
        context: { hr_expense_refuse_model: 'hr.expense.sheet' },
        fields: { reason: null },
        formItems: [{ type: 'input', name: 'reason', label: '理由' }],
        method: 'expense_refuse_reason'
      }
    }
  },

  transfers: {
    draft: {
      actions: ['update', 'submit'],
      states: ['draft', 'submit', 'approve', 'post', 'done']
    },
    submit: {
      actions: ['approve', 'draft', 'cancel'],
      states: ['draft', 'submit', 'approve', 'post', 'done']
    },
    approve: {
      actions: ['post', 'cancel'],
      states: ['draft', 'submit', 'approve', 'post', 'done']
    },
    post: {
      actions: ['done'],
      states: ['draft', 'submit', 'approve', 'post', 'done']
    },
    done: {
      actions: [],
      states: ['draft', 'submit', 'approve', 'post', 'done']
    },
    cancel: {
      actions: ['draft'],
      states: {
        submit: ['draft', 'submit', 'cancel'],
        approve: ['draft', 'submit', 'approve', 'cancel']
      }
    }
  }
}

const Model = {
  configs: {
    workflow,
    values_default,
    domain_default,
    fields_default,
    filterItems,
    columns,
    formItems,
    child_configs
  },

  extend: BaseClass => {
    class ModelClass extends BaseClass {
      global_domain(domain_src = {}) {
        // console.log('this.global_domain in odoo.res.partner')
        const domain = { ...domain_src }
        // const userinfo = self.get_userinfo()
        // const { uid } = userinfo
        // domain.user_id = uid
        return super.global_domain(domain)
      }
      async reset_expense_sheets(rid) {
        await this.call('reset_expense_sheets', [rid])
        return true
      }

      async refuse_sheet(rid) {
        //
      }

      async action_submit_sheet(rid) {
        await this.call('action_submit_sheet', [rid])
        return true
      }
      async approve_expense_sheets(rid) {
        await this.call('approve_expense_sheets', [rid])
        return true
      }
      async action_sheet_move_create(rid) {
        await this.call('action_sheet_move_create', [rid])
        return true
      }

      async paid_expense_sheets(rid, kwargs) {
        //
      }

      async create(values) {
        const rid = await super.create(values)
        await this.call('_onchange_employee_id', [rid])
        return rid
      }
      async write(values) {
        const ret = await super.write(values)
        await this.call('_onchange_employee_id', [values.id])
        return ret
      }
    }
    return ModelClass
  }
}

export default Model
